﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using Volo.Abp.Auditing;
using Volo.Abp.Data;
using Volo.Abp.Domain.Entities;
using Volo.Abp.ObjectExtending;

namespace DomainEventDemo.Extensions
{
    [Serializable]
    public abstract class ExtensionAggregateRoot<TKey> : ExtensionBasicAggregateRoot<TKey>, IHasExtraProperties, IHasConcurrencyStamp
    {
        [NonSerialized]
        private string concurrencyStamp;
        [NonSerialized]
        private Dictionary<string, object> extraProperties;

        protected ExtensionAggregateRoot()
        {
            ConcurrencyStamp = Guid.NewGuid().ToString("N");
            ExtraProperties = new Dictionary<string, object>();
            this.SetDefaultsForExtraProperties();
        }

        protected ExtensionAggregateRoot(TKey id)
            : base(id)
        {
            ConcurrencyStamp = Guid.NewGuid().ToString("N");
            ExtraProperties = new Dictionary<string, object>();
            this.SetDefaultsForExtraProperties();
        }

        [DisableAuditing]
        public virtual string ConcurrencyStamp { get => concurrencyStamp; set => concurrencyStamp = value; }

        public virtual Dictionary<string, object> ExtraProperties { get => extraProperties; protected set => extraProperties = value; }

        public virtual void SetConcurrencyStamp(string concurrencyStamp)
        {
            ConcurrencyStamp = concurrencyStamp;
        }

        public virtual IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
        {
            return ExtensibleObjectValidator.GetValidationErrors(
                this,
                validationContext
            );
        }
    }
}
